<?php


namespace App\Http\Controllers\admin\service;

use App\Http\Controllers\admin\model\AdminModel;
use App\Http\Controllers\admin\model\AdminSnModel;
use App\Http\Controllers\admin\model\AppGroundModel;
use App\Http\Controllers\admin\model\BatchModel;
use App\Http\Controllers\admin\model\BrowserModel;
use App\Http\Controllers\admin\model\LabelModel;
use App\Http\Controllers\admin\model\ParentAppModel;
use App\Http\Controllers\admin\model\ParentSnModel;
use App\Http\Controllers\admin\model\SettingModel;
use App\Http\Controllers\admin\model\TeacherAppModel;
use App\Http\Controllers\admin\model\TfMediaModel;
use App\Http\Controllers\utils\LogUtil;
use App\Http\Controllers\utils\ToolUtil;
use Illuminate\Support\Facades\DB;

/**
 * 管控部署
 * Class ControlApi
 * @package App\Http\Controllers\admin\service
 */
class ControlService
{
    private static function handleSnName($data){
        foreach ($data as $k=>$v){
            $v['snName'] = AdminSnModel::queryFirst(['value'=>$v['sn_value']],['name'])['name'];
            $data[$k] = $v;
        }
        return $data;
    }

    /**
     * 拼接https
     * @param $param
     * @return mixed
     */
    public static function handleHttpsOrHttp($param){
        if(!strstr($param['homePage'],'http')){
            $param['homePage'] = 'https://'.$param['homePage'];
        }
        if(!strstr($param['labelPage'],'http')){
            $param['labelPage'] = 'https://'.$param['labelPage'];
        }
        return $param;
    }

    /**
     * 获取老师班级名称
     * @param $param
     * @return false|string
     */
    public static function queryBatchName($param){
        $data = BatchModel::queryGet(['admin_id'=>$param['userId']],['id','name']);
        if(count($data)){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }


    /**
     * 获取家长sn码
     * @param $param
     * @return false|string
     */
    public static function querySnName($param){
        $data = ParentSnModel::queryLeftGet(['p.admin_id'=>$param['userId']],['a.id','a.value','a.name']);
        $res = [];
        foreach ($data as $key => $value) {
            if(!empty($value['id'])){
                $res[] = $value;
            }
        }
        if(!empty($res)){
            return ToolUtil::returnJson(200,'成功',$res);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理获取全部的数据
     * @param $data
     * @return mixed
     */
    public static function checkDataAll($data){
        foreach ($data['data'] as $key=>$value){
            $batchArr = explode(',',$value['batch_id']);
            $batchNameArr = [];
            foreach ($batchArr as $k=>$v){
                $batchNameArr[$k] = BatchModel::whereField(['id'=>$v],'name');
            }
            $batchStr = implode(',',$batchNameArr);
            $value['batch_id'] = $batchStr;
            $data['data'][$key] = $value;
        }
        return $data;
    }

    /**
     * 处理编号获取的数据
     * @param $data
     * @return mixed
     */
    public static function checkDataId($data){
        $batchArr = explode(',',$data['batch_id']);
        $batchNameArr = [];
        foreach ($batchArr as $k=>$v){
            $batchNameArr[$k] = BatchModel::whereField(['id'=>$v],'name');
        }
        $batchStr = implode(',',$batchNameArr);
        $data['batch_id'] = $batchStr;
        return $data;
    }


    /**
     * 增加老师浏览器上网管控和主页书签
     * @param $param
     * @return false|string
     */
    public static function addTeacherBrowser($param){

        $bid = ToolUtil::filterStr($param['bId']);
        $batchArr = explode(',',$bid);
        $type = (int)$param['type'];
        if($type === 0){

            foreach ($batchArr as $key => $value) {
                $browser = DB::table('t_browser')
                    ->where(['address'=>$param['address']])
                    ->whereRaw(" ( type = 0 or type = 1 ) ")
                    ->whereRaw(" find_in_set('{$value}',batch_ids) ")
                    ->first(['id']);
                if($browser){
                    return ToolUtil::returnJson(301,'不能添加重复网址');
                }                
            }

            $desc = '添加黑名单浏览器网址';
            $contentType = 6;

        }else if($type === 1){

            foreach ($batchArr as $key => $value) {
                $browser = DB::table('t_browser')
                    ->where(['address'=>$param['address']])
                    ->whereRaw(" ( type = 0 or type = 1 ) ")
                    ->whereRaw(" find_in_set('{$value}',batch_ids) ")
                    ->first(['id']);
                if($browser){
                    return ToolUtil::returnJson(301,'不能添加重复网址');
                }                
            }

            $desc = '添加白名单浏览器网址';
            $contentType = 6;

        }else if($type === 2){

            foreach ($batchArr as $key => $value) {
                $browser = DB::table('t_browser')
                    ->where(['type'=>2])
                    ->whereRaw(" find_in_set('{$value}',batch_ids) ")
                    ->first(['id']);
                if($browser){
                    return ToolUtil::returnJson(301,'不能重复添加主页');
                }                
            }

            $desc = '添加浏览器主页';
            $contentType = 18;

        }else if($type === 3){

            foreach ($batchArr as $key => $value) {
                $browser = DB::table('t_browser')
                    ->where(['address'=>$param['address']])
                    ->where(['type'=>3])
                    ->whereRaw(" find_in_set('{$value}',batch_ids) ")
                    ->first(['id']);
                if($browser){
                    return ToolUtil::returnJson(301,'不能重复添加书签');
                }                
            }

            $desc = '添加浏览器书签';
            $contentType = 18;

        }else{
            return ToolUtil::returnJson(300,'类型错误');
        }

        $data = [
            'address'=>$param['address'],
            'type'=>$type,
            'create_time'=>time(),
            'batch_ids'=>$bid,
            'admin_id'=>$param['userId']
        ];
        $int = BrowserModel::add($data);
        if($int){
            $param['remark'] = $desc;
            $param['details'] = ToolUtil::handleSuccessOrError(['address'=>$param['address']]);
            LogUtil::addAdminLog($param);
            $param['type'] = 1;
            $param['contentType'] = $contentType;
            $param['batch'] = $batchArr;
            $param['statusType'] = $desc;
            $jPush = LogUtil::addPushLog($param);
            if(in_array(200,$jPush)){
                return ToolUtil::returnJson(200,'成功');
            }
            return ToolUtil::returnJson(200,'成功,推送失败');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 获取老师浏览器上网管控和主页书签
     * @param $param
     * @return false|string
     */
    public static function queryTeacherBrowser($param)
    {
        $admin = AdminModel::queryFirst(['id' => $param['userId']], ['group_id']);
        $field = ['b.id', 'b.address', 'b.type', 'b.create_time', 'a.username', 'batch_ids as batch_id'];
        if ($admin['group_id'] == 1 || $admin['group_id'] == 2) {
            $param['notnull'] = 'b.batch_ids';
            $where = [];
            switch ($param['type']) {
                case 1:
                    $data = self::handleQueryBrowser($where, $param, $field);
                    break;
                case 2:
                    $param['field'] = 'b.address';
                    $param['b.address'] = $param['address'];
                    $data = self::handleQueryBrowser($where, $param, $field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = self::handleQueryBrowser($where, $param, $field);
                    break;
            }
        } elseif ($admin['group_id'] == 3) {
            $where['b.admin_id'] = $param['userId'];
            switch ($param['type']) {
                case 1:
                    $data = self::handleQueryBrowser($where, $param, $field);
                    break;
                case 2:
                    $param['field'] = 'b.address';
                    $param['b.address'] = $param['address'];
                    $data = self::handleQueryBrowser($where, $param, $field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = self::handleQueryBrowser($where, $param, $field);
                    break;
            }
        } else {
            return ToolUtil::returnJson(400, '没有数据');
        }
        if(count($data['data'])){
            $result = ToolUtil::handleBatch($data['data'],'batch_id');
            return ToolUtil::returnJson(200,'成功',$result,$data['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理获取浏览器上网管控和主页书签信息
     * @param array $where
     * @param $param
     * @param $field
     * @return mixed
     */
    public static function handleQueryBrowser($where=[],$param,$field){
        if($where){
            $data['count'] = BrowserModel::queryCount($where,$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = BrowserModel::queryAll($where,$param,$page,$field);
        }else{
            $data['count'] = BrowserModel::queryCount([],$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = BrowserModel::queryAll([],$param,$page,$field);
        }
        return $data;
    }

    /**
     * 编号获取老师浏览器上网管控和主页书签
     * @param $param
     * @return false|string
     */
    public static function queryIdTeacherBrowser($param){
        $field = ['b.id','b.address','b.type','b.create_time','a.username','batch_ids as batch_id'];
        $data = BrowserModel::queryLeftFirst(['b.id'=>$param['id']],$field);
        if($data){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编辑老师浏览器上网管控和主页书签
     * @param $param
     * @return false|string
     */
    public static function editTeacherBrowser($param){

        $bid = ToolUtil::filterStr($param['bId']);
        $batchArr = explode(',',$bid);
        $type = (int)$param['type'];
        $id = (int)$param['id'];
        $res = DB::table('t_browser')->where(['id'=>$id])->first(['id','batch_ids']);
        if(empty($res)){
            return ToolUtil::returnJson(301,'没有记录');
        }

        if($type === 0){

            foreach ($batchArr as $key => $value) {
                $browser = DB::table('t_browser')
                    ->where(['address'=>$param['address']])
                    ->whereRaw(" ( type = 0 or type = 1 ) ")
                    ->whereRaw(" find_in_set('{$value}',batch_ids) ")
                    ->first(['id']);
                if($browser && $browser['id'] != $id){
                    return ToolUtil::returnJson(301,'不能添加重复网址');
                }
            }

            $desc = '编辑黑名单浏览器网址';
            $contentType = 6;

        }else if($type === 1){

            foreach ($batchArr as $key => $value) {
                $browser = DB::table('t_browser')
                    ->where(['address'=>$param['address']])
                    ->whereRaw(" ( type = 0 or type = 1 ) ")
                    ->whereRaw(" find_in_set('{$value}',batch_ids) ")
                    ->first(['id']);
                if($browser && $browser['id'] != $id){
                    return ToolUtil::returnJson(301,'不能添加重复网址');
                }
            }

            $desc = '编辑白名单浏览器网址';
            $contentType = 6;

        }else if($type === 2){

            foreach ($batchArr as $key => $value) {
                $browser = DB::table('t_browser')
                    ->where(['type'=>2])
                    ->whereRaw(" find_in_set('{$value}',batch_ids) ")
                    ->first(['id']);
                if($browser && $browser['id'] != $id){
                    return ToolUtil::returnJson(300,'不能重复添加主页');
                }
            }

            $desc = '小程序编辑浏览器主页';
            $contentType = 18;

        }else if($type === 3){

            foreach ($batchArr as $key => $value) {
                $browser = DB::table('t_browser')
                    ->where(['address'=>$param['address']])
                    ->where(['type'=>3])
                    ->whereRaw(" find_in_set('{$value}',batch_ids) ")
                    ->first(['id']);
                if($browser && $browser['id'] != $id){
                    return ToolUtil::returnJson(301,'不能重复添加书签');
                }                
            }

            $desc = '小程序编辑浏览器书签';
            $contentType = 18;

        }else{
            return ToolUtil::returnJson(300,'类型错误');
        }

        $res = BrowserModel::edit(['id'=>$id],['batch_ids'=>$bid,'type'=>$type,'address'=>$param['address'],'update_time'=>time()]);
        if(empty($res)){
            return ToolUtil::returnJson(300,'失败');
        }

        $param['type'] = 1;
        $param['contentType'] = $contentType;
        $param['statusType'] = $desc;

        $batchArr2 = explode(',',$res['batch_ids']);
        $batchArr3 = array_diff($batchArr2,$batchArr);
        if(!empty($batchArr3)){
            $param['batch'] = $batchArr3;
            LogUtil::addPushLog($param);
        }

        $param['batch'] = $batchArr;
        $param['remark'] = $desc;
        $param['details'] = ToolUtil::handleSuccessOrError(['address'=>$param['address']]);
        LogUtil::addAdminLog($param);
        $jPush = LogUtil::addPushLog($param);
        if(in_array(200,$jPush)){
            return ToolUtil::returnJson(200,'成功');
        }else{
            return ToolUtil::returnJson(200,'成功,推送失败');
        }

    }

    /**
     * 删除老师浏览器上网管控和主页书签
     * @param $param
     * @return false|string
     */
    public static function deleteTeacherBrowser($param){
        $browser = BrowserModel::queryFirst(['id'=>$param['id']],['batch_ids','address','type']);
        if($browser){

            if($browser['type'] === 0){

                $desc = '删除浏览器黑名单网址';
                $contentType = 6;

            }else if($browser['type'] === 1){

                $desc = '删除浏览器白名单网址';
                $contentType = 6;

            }else if($browser['type'] === 2){

                $desc = '删除浏览器主页';
                $contentType = 18;

            }else if($browser['type'] === 3){

                $desc = '删除浏览器书签';
                $contentType = 18;

            }else{
                return ToolUtil::returnJson(300,'类型错误');
            }

            $batchArr = explode(',',$browser['batch_ids']);
            $int = BrowserModel::delete(['id'=>$param['id']]);
            if($int){
                $param['remark'] = $desc;
                $param['details'] = ToolUtil::handleSuccessOrError(['address'=>$browser['address']]);
                LogUtil::addAdminLog($param);
                $param['type'] = 1;
                $param['contentType'] = $contentType;
                $param['batch'] = $batchArr;
                $param['statusType'] = $desc;
                $jPush = LogUtil::addPushLog($param);
                if(in_array(200,$jPush)){
                    return ToolUtil::returnJson(200,'成功');
                }
                return ToolUtil::returnJson(200,'成功,推送失败');
            }
            return ToolUtil::returnJson(300,'失败');
        }
        return ToolUtil::returnJson(301,'没有该信息');
    }

    /**
     * 增加家长浏览器上网管控和主页书签
     * @param $param
     * @return false|string
     */
    public static function addParentBrowser($param){

        $type = (int)$param['type'];
        $sn = ToolUtil::filterStr($param['snValue']);
        if(strlen($sn) > 12){
            return ToolUtil::returnJson(301,'sn码长度超过12位');
        }
        if($type === 0){

            $browser = DB::table('t_browser')
                ->where(['sn_value'=>$sn,'address'=>$param['address']])
                ->whereRaw(" ( type = 0 or type = 1 ) ")
                ->first(['id']);
            if($browser){
                return ToolUtil::returnJson(301,'不能添加重复网址');
            }

            $desc = '添加黑名单浏览器网址';
            $contentType = 6;

        }else if($type === 1){

            $browser = DB::table('t_browser')
                ->where(['sn_value'=>$sn,'address'=>$param['address']])
                ->whereRaw(" ( type = 0 or type = 1 ) ")
                ->first(['id']);
            if($browser){
                return ToolUtil::returnJson(301,'不能添加重复网址');
            }

            $desc = '添加白名单浏览器网址';
            $contentType = 6;

        }else if($type === 2){

            $browser = BrowserModel::queryFirst(['sn_value'=>$sn,'type'=>2],['id']);
            if($browser){
                return ToolUtil::returnJson(300,'不能重复添加主页');
            }

            $desc = '添加浏览器主页';
            $contentType = 18;

        }else if($type === 3){

            $browser = BrowserModel::queryFirst(['sn_value'=>$sn,'type'=>3,'address'=>$param['address']],['id']);
            if($browser){
                return ToolUtil::returnJson(300,'不能重复添加书签');
            }

            $desc = '添加浏览器书签';
            $contentType = 18;

        }else{
            return ToolUtil::returnJson(300,'类型错误');
        }

        $res = BrowserModel::add(['type'=>$type,'admin_id'=>$param['userId'],'address'=>$param['address'],'sn_value'=>$sn,'create_time'=>time()]);
        if(empty($res)){
            return ToolUtil::returnJson(300,'失败');
        }

        $param['type'] = 2;
        $param['contentType'] = $contentType;
        $param['statusType'] = $desc;
        $param['batch'] = array($sn);
        $param['remark'] = $desc;
        $param['details'] = ToolUtil::handleSuccessOrError(['address'=>$param['address']]);
        LogUtil::addAdminLog($param);
        $jPush = LogUtil::addPushLog($param);
        if(in_array(200,$jPush)){
            return ToolUtil::returnJson(200,'成功');
        }else{
            return ToolUtil::returnJson(200,'成功,推送失败');
        }


    }

    /**
     * 获取家长浏览器上网管控和主页书签
     * @param $param
     * @return false|string
     */
    public static function queryParentBrowser($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        $field = ['b.id','b.address','b.type','b.create_time','a.username','sn_value'];
        if($admin['group_id']==1 || $admin['group_id']==2){
            $param['notnull'] = 'b.sn_value';
            $where = [];
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryBrowser($where,$param,$field);
                    break;
                case 2:
                    $param['field'] = 'b.address';
                    $param['b.address'] = $param['address'];
                    $data = self::handleQueryBrowser($where,$param,$field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = self::handleQueryBrowser($where,$param,$field);
                    break;
            }
        }elseif($admin['group_id']==4){
            $where['b.admin_id'] = $param['userId'];
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryBrowser($where,$param,$field);
                    break;
                case 2:
                    $param['field'] = 'b.address';
                    $param['b.address'] = $param['address'];
                    $data = self::handleQueryBrowser($where,$param,$field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = self::handleQueryBrowser($where,$param,$field);
                    break;
            }
        }else{
            return ToolUtil::returnJson(400,'没有数据');
        }
        if(count($data['data'])){
            $result = self::handleSnName($data['data']);
            return ToolUtil::returnJson(200,'成功',$result,$data['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编号获取家长浏览器上网管控和主页书签
     * @param $param
     * @return false|string
     */
    public static function queryIdParentBrowser($param){
        $field = ['b.id','b.address','b.type','b.create_time','a.username','b.sn_value'];
        $data = BrowserModel::queryLeftFirst(['b.id'=>$param['id']],$field);
        if($data){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编辑家长浏览器上网管控和主页书签
     * @param $param
     * @return false|string
     */
    public static function editParentBrowser($param){

        $type = (int)$param['type'];
        $id = (int)$param['id'];
        $sn = ToolUtil::filterStr($param['snValue']);
        if(strlen($sn) > 12){
            return ToolUtil::returnJson(301,'sn码长度超过12位');
        }
        if($type === 0){

            $browser = DB::table('t_browser')
                ->where(['sn_value'=>$sn,'address'=>$param['address']])
                ->whereRaw(" ( type = 0 or type = 1 ) ")
                ->first(['id']);
            if($browser && $browser['id'] != $id){
                return ToolUtil::returnJson(301,'不能添加重复网址');
            }

            $desc = '编辑黑名单浏览器网址';
            $contentType = 6;

        }else if($type === 1){

            $browser = DB::table('t_browser')
                ->where(['sn_value'=>$sn,'address'=>$param['address']])
                ->whereRaw(" ( type = 0 or type = 1 ) ")
                ->first(['id']);
            if($browser && $browser['id'] != $id){
                return ToolUtil::returnJson(301,'不能添加重复网址');
            }

            $desc = '编辑白名单浏览器网址';
            $contentType = 6;

        }else if($type === 2){

            $browser = BrowserModel::queryFirst(['sn_value'=>$sn,'type'=>2],['id']);
            if($browser && $browser['id'] != $id){
                return ToolUtil::returnJson(300,'不能重复添加主页');
            }

            $desc = '编辑浏览器主页';
            $contentType = 18;

        }else if($type === 3){

            $browser = BrowserModel::queryFirst(['sn_value'=>$sn,'type'=>3,'address'=>$param['address']],['id']);
            if($browser && $browser['id'] != $id){
                return ToolUtil::returnJson(300,'不能重复添加书签');
            }

            $desc = '编辑浏览器书签';
            $contentType = 18;

        }else{
            return ToolUtil::returnJson(300,'类型错误');
        }

        $res = BrowserModel::edit(['id'=>$id],['sn_value'=>$sn,'type'=>$type,'address'=>$param['address'],'update_time'=>time()]);
        if(empty($res)){
            return ToolUtil::returnJson(300,'失败');
        }

        $param['type'] = 2;
        $param['contentType'] = $contentType;
        $param['statusType'] = $desc;
        $param['batch'] = array($sn);
        $param['remark'] = $desc;
        $param['details'] = ToolUtil::handleSuccessOrError(['address'=>$param['address']]);
        LogUtil::addAdminLog($param);
        $jPush = LogUtil::addPushLog($param);
        if(in_array(200,$jPush)){
            return ToolUtil::returnJson(200,'成功');
        }else{
            return ToolUtil::returnJson(200,'成功,推送失败');
        }

    }

    /**
     * 删除家长浏览器管控
     * @param $param
     * @return false|string
     */
    public static function deleteParentBrowser($param){

        $id = (int)$param['id'];
        $browser = BrowserModel::queryFirst(['id'=>$id],['sn_value','type','address']);
        if(empty($browser)){
            return ToolUtil::returnJson(300,'编号错误');
        }

        if($browser['type'] === 0){

            $desc = '删除浏览器黑名单网址';
            $contentType = 6;

        }else if($browser['type'] === 1){

            $desc = '删除浏览器白名单网址';
            $contentType = 6;

        }else if($browser['type'] === 2){

            $desc = '删除浏览器主页';
            $contentType = 18;

        }else if($browser['type'] === 3){

            $desc = '删除浏览器书签';
            $contentType = 18;

        }else{
            return ToolUtil::returnJson(300,'类型错误');
        }

        $param['type'] = 2;
        $param['batch'] = array($browser['sn_value']);
        $param['details'] = ToolUtil::handleSuccessOrError(['address'=>$browser['address']]);

        if(BrowserModel::delete(['id'=>$id])){

            $param['contentType'] = $contentType;
            $param['remark'] = $desc;
            LogUtil::addAdminLog($param);
            $param['statusType'] = $desc;
            $jPush = LogUtil::addPushLog($param);
            if(in_array(200,$jPush)){
                return ToolUtil::returnJson(200,'成功');
            }else{
                return ToolUtil::returnJson(200,'成功,推送失败');
            }

        }

        return ToolUtil::returnJson(300,'失败');

    }

    /**
     * 增加老师应用第三方跳转
     * @param $param
     * @return false|string
     */
    public static function addTeacherAppGround($param){
        
        $teacherApp = TeacherAppModel::queryFirst(['app_package'=>$param['pakName'],'admin_id'=>$param['userId']],['id']);
        if($teacherApp){
            $appGround = AppGroundModel::queryFirst(['batch_id'=>$param['bId'],'app_id'=>$teacherApp['id'],'admin_id'=>$param['userId']],['id']);
            if($appGround){
                return ToolUtil::returnJson(301,'这个班级存在相同包名');
            }
        }else{
            return ToolUtil::returnJson(301,'不存在该包名');
        }

        $batchArr = explode(',',$param['bId']);
        $data = [
            'app_id'=>$teacherApp['id'],
            'address'=>$param['address'],
            'batch_id'=>$param['bId'],
            'admin_id'=>$param['userId'],
            'create_time'=>time()
        ];
        $int = AppGroundModel::add($data);
        if($int){
            $param['remark'] = '增加第三方应用跳转';
            $param['details'] = ToolUtil::handleSuccessOrError(['address'=>$param['address'],'pakName'=>$param['pakName']]);
            LogUtil::addAdminLog($param);
            $param['type'] = 1;
            $param['contentType'] = 16;
            $param['status'] = 1;
            $param['title'] = 'Ok';
            $param['batch'] = $batchArr;
            $param['statusType'] = '增加第三方应用跳转';
            $jPush = LogUtil::addPushLog($param);
            if(in_array(200,$jPush)){
                return ToolUtil::returnJson(200,'成功');
            }
            return ToolUtil::returnJson(200,'成功,推送失败');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 获取老师第三方应用跳转
     * @param $param
     * @return false|string
     */
    public static function queryTeacherAppGround($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        $field = ['ag.id','t.app_package','ag.address','ag.batch_id','a.username','ag.create_time'];
        if($admin['group_id']==1 || $admin['group_id']==2){
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryTeacherAppGround([],$param,$field);
                    break;
                case 2:
                    $param['field'] = 't.app_package';
                    $param['t.app_package'] = $param['pakName'];
                    $data = self::handleQueryTeacherAppGround([],$param,$field);
                    break;
                case 3:
                    $param['field'] = 'ag.address';
                    $param['ag.address'] = $param['address'];
                    $data = self::handleQueryTeacherAppGround([],$param,$field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = self::handleQueryTeacherAppGround([],$param,$field);
                    break;
            }
        }elseif($admin['group_id']==3){
            $where['ag.admin_id'] = $param['userId'];
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryTeacherAppGround($where,$param,$field);
                    break;
                case 2:
                    $param['field'] = 't.app_package';
                    $param['t.app_package'] = $param['pakName'];
                    $data = self::handleQueryTeacherAppGround($where,$param,$field);
                    break;
                case 3:
                    $param['field'] = 'ag.address';
                    $param['ag.address'] = $param['address'];
                    $data = self::handleQueryTeacherAppGround($where,$param,$field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = self::handleQueryTeacherAppGround($where,$param,$field);
                    break;
            }
        }else{
            return ToolUtil::returnJson(400,'没有数据');
        }
        if(count($data['data'])){
            $result = self::checkDataAll($data);
            return ToolUtil::returnJson(200,'成功',$result['data'],$result['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理获取老师第三方应用跳转
     * @param array $where
     * @param $param
     * @param $field
     * @return mixed
     */
    public static function handleQueryTeacherAppGround($where=[],$param,$field){
        if($where){
            $data['count'] = AppGroundModel::queryCount($where,$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = AppGroundModel::queryAll($where,$param,$page,$field);
        }else{
            $data['count'] = AppGroundModel::queryCount([],$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = AppGroundModel::queryAll([],$param,$page,$field);
        }
        return $data;
    }

    /**
     * 处理获取家长第三方应用跳转
     * @param array $where
     * @param $param
     * @param $field
     * @return mixed
     */
    public static function handleQueryParentAppGround($where=[],$param,$field){
        if($where){
            $data['count'] = AppGroundModel::queryParentCount($where,$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = AppGroundModel::queryParentAll($where,$param,$page,$field);
        }else{
            $data['count'] = AppGroundModel::queryParentCount([],$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = AppGroundModel::queryParentAll([],$param,$page,$field);
        }
        return $data;
    }

    /**
     * 编号获取老师第三方应用跳转
     * @param $param
     * @return false|string
     */
    public static function queryIdTeacherAppGround($param){
        $field = ['ag.id','t.app_package','ag.address','ag.batch_id','a.username'];
        $data = AppGroundModel::queryLeftFirst(['ag.id'=>$param['id']],$field);
        if($data){
            $res = self::checkDataId($data);
            return ToolUtil::returnJson(200,'成功',$res);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编辑老师第三方应用跳转
     * @param $param
     * @return false|string
     */
    public static function editTeacherAppGround($param){

        $batchArr = explode(',',$param['bId']);
        $data = [
            'address'=>$param['address'],
            'update_time'=>time()
        ];
        $int = AppGroundModel::edit(['id'=>$param['id']],$data);
        if($int){
            $param['remark'] = '编辑第三方应用跳转';
            $param['type'] = 1;
            $param['status'] = 1;
            $param['contentType'] = 16;
            $param['title'] = 'OK';
            $param['statusType'] = '编辑第三方应用跳转';
            $param['batch'] = $batchArr;
            $param['details'] = ToolUtil::handleSuccessOrError(['address'=>$param['address']]);
            LogUtil::addAdminLog($param);
            $jPush = LogUtil::addPushLog($param);
            if(in_array(200,$jPush)){
                return ToolUtil::returnJson(200,'成功');
            }
            return ToolUtil::returnJson(200,'成功,推送失败');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 删除老师第三方跳转应用
     * @param $param
     * @return false|string
     */
    public static function deleteTeacherAppGround($param){
        $appGround = AppGroundModel::queryLeftFirst(['ag.id'=>$param['id']],['ag.batch_id','t.app_package','ag.address']);
        if($appGround){
            $int = AppGroundModel::delete(['id'=>$param['id']]);
            if($int){
                $batchIdArr = explode(',',$appGround['batch_id']);
                $param['remark'] = '删除第三方应用跳转';
                $param['type'] = 1;
                $param['status'] = 1;
                $param['contentType'] = 16;
                $param['title'] = 'OK';
                $param['statusType'] = '删除第三方应用跳转';
                $param['batch'] = $batchIdArr;
                $param['details'] = ToolUtil::handleSuccessOrError(['address'=>$appGround['address']]);
                LogUtil::addAdminLog($param);
                $jPush = LogUtil::addPushLog($param);
                if(in_array(200,$jPush)){
                    return ToolUtil::returnJson(200,'成功');
                }
                return ToolUtil::returnJson(200,'成功,推送失败');
            }
            return ToolUtil::returnJson(300,'失败');
        }
        return ToolUtil::returnJson(301,'没有该信息');
    }

    /**
     * 增加家长第三方应用跳转
     * @param $param
     * @return false|string
     */
    public static function addParentAppGround($param){
        $parentApp = ParentAppModel::queryFirst(['app_package'=>$param['pakName'],'admin_id'=>$param['userId']],['id']);
        if($parentApp){
            $appGround = AppGroundModel::queryFirst(['sn_value'=>$param['snValue'],'app_id'=>$parentApp['id'],'admin_id'=>$param['userId']],['id']);
            if($appGround){
                return ToolUtil::returnJson(301,'这个SN码存在相同包名');
            }
            $data = [
                'app_id'=>$parentApp['id'],
                'address'=>$param['address'],
                'sn_value'=>$param['snValue'],
                'admin_id'=>$param['userId'],
                'create_time'=>time()
            ];
            $int = AppGroundModel::add($data);
            if($int){
                $param['remark'] = '增加第三方应用跳转';
                $param['details'] = ToolUtil::handleSuccessOrError(['address'=>$param['address'],'pakName'=>$param['pakName']]);
                LogUtil::addAdminLog($param);
                $param['type'] = 2;
                $param['contentType'] = 16;
                $param['status'] = 1;
                $param['title'] = 'Ok';
                $param['batch'] = array($param['snValue']);
                $param['statusType'] = '增加第三方应用跳转';
                $jPush = LogUtil::addPushLog($param);
                if(in_array(200,$jPush)){
                    return ToolUtil::returnJson(200,'成功');
                }
                return ToolUtil::returnJson(200,'成功,推送失败');
            }
            return ToolUtil::returnJson(300,'失败');
        }
        return ToolUtil::returnJson(301,'不存在该包名');
    }

    /**
     * 获取家长第三方应用跳转
     * @param $param
     * @return false|string
     */
    public static function queryParentAppGround($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        $field = ['ag.id','t.app_package','ag.address','ag.sn_value','a.username','ag.create_time'];
        if($admin['group_id']==1 || $admin['group_id']==2){
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryParentAppGround([],$param,$field);
                    break;
                case 2:
                    $param['field'] = 't.app_package';
                    $param['t.app_package'] = $param['pakName'];
                    $data = self::handleQueryParentAppGround([],$param,$field);
                    break;
                case 3:
                    $param['field'] = 'ag.address';
                    $param['ag.address'] = $param['address'];
                    $data = self::handleQueryParentAppGround([],$param,$field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = self::handleQueryParentAppGround([],$param,$field);
                    break;
            }
        }elseif($admin['group_id']==4){
            $where['ag.admin_id'] = $param['userId'];
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryParentAppGround($where,$param,$field);
                    break;
                case 2:
                    $param['field'] = 't.app_package';
                    $param['t.app_package'] = $param['pakName'];
                    $data = self::handleQueryParentAppGround($where,$param,$field);
                    break;
                case 3:
                    $param['field'] = 'ag.address';
                    $param['ag.address'] = $param['address'];
                    $data = self::handleQueryParentAppGround($where,$param,$field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = self::handleQueryParentAppGround($where,$param,$field);
                    break;
            }
        }else{
            return ToolUtil::returnJson(400,'没有数据');
        }
        if(count($data['data'])){
            $result = self::handleSnName($data['data']);
            return ToolUtil::returnJson(200,'成功',$result,$data['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编号获取家长第三方应用跳转
     * @param $param
     * @return false|string
     */
    public static function queryIdParentAppGround($param){
        $field = ['ag.id','t.app_package','ag.address','ag.sn_value','a.username'];
        $data = AppGroundModel::queryLeftParentFirst(['ag.id'=>$param['id']],$field);
        if($data){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编辑家长第三方应用跳转
     * @param $param
     * @return false|string
     */
    public static function editParentAppGround($param){

        $data = [
            'address'=>$param['address'],
            'update_time'=>time()
        ];
        $int = AppGroundModel::edit(['id'=>$param['id']],$data);
        if($int){
            $param['remark'] = '编辑第三方应用跳转';
            $param['type'] = 2;
            $param['status'] = 1;
            $param['contentType'] = 16;
            $param['title'] = 'OK';
            $param['statusType'] = '编辑第三方应用跳转';
            $param['batch'] = array($param['snValue']);
            $param['details'] = ToolUtil::handleSuccessOrError(['address'=>$param['address']]);
            LogUtil::addAdminLog($param);
            $jPush = LogUtil::addPushLog($param);
            if(in_array(200,$jPush)){
                return ToolUtil::returnJson(200,'成功');
            }
            return ToolUtil::returnJson(200,'成功,推送失败');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 删除第三方应用跳转
     * @param $param
     * @return false|string
     */
    public static function deleteParentAppGround($param){
        $appGround = AppGroundModel::queryLeftFirst(['ag.id'=>$param['id']],['ag.sn_value','t.app_package','ag.address']);
        if($appGround){
            $int = AppGroundModel::delete(['id'=>$param['id']]);
            if($int){
                $param['remark'] = '删除第三方应用跳转';
                $param['type'] = 2;
                $param['status'] = 1;
                $param['contentType'] = 16;
                $param['title'] = 'OK';
                $param['statusType'] = '删除第三方应用跳转';
                $param['batch'] = array($appGround['sn_value']);
                $param['details'] = ToolUtil::handleSuccessOrError(['address'=>$appGround['address']]);
                LogUtil::addAdminLog($param);
                $jPush = LogUtil::addPushLog($param);
                if(in_array(200,$jPush)){
                    return ToolUtil::returnJson(200,'成功');
                }
                return ToolUtil::returnJson(200,'成功,推送失败');
            }
            return ToolUtil::returnJson(300,'失败');
        }
        return ToolUtil::returnJson(301,'没有该信息');
    }

    /**
     * 获取老师应用自启、升级、联网
     * @param $param
     * @return false|string
     */
    public static function queryTeacherAppStart($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        $field = ['t.id','t.app_name','t.app_package','a.username','t.bind_batch_id as batch_id','t.app_img','t.is_auto',
            't.is_network','t.is_upgrade','t.is_slide'];
        if($admin['group_id']==1 || $admin['group_id']==2){
            switch ($param['type']){
                case 1:
                    $data = AppService::handleQueryTeacherApp([],$param,$field);
                    break;
                case 2:
                    $param['field'] = 't.app_name';
                    $param['t.app_name'] = $param['appName'];
                    $data = AppService::handleQueryTeacherApp([],$param,$field);
                    break;
                case 3:
                    $param['field'] = 't.app_package';
                    $param['t.app_package'] = $param['pakName'];
                    $data = AppService::handleQueryTeacherApp([],$param,$field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = AppService::handleQueryTeacherApp([],$param,$field);
                    break;
            }
        }elseif($admin['group_id']==3){
            $where['t.admin_id'] = $param['userId'];
            switch ($param['type']){
                case 1:
                    $data = AppService::handleQueryTeacherApp($where,$param,$field);
                    break;
                case 2:
                    $param['field'] = 't.app_name';
                    $param['t.app_name'] = $param['appName'];
                    $data = AppService::handleQueryTeacherApp($where,$param,$field);
                    break;
                case 3:
                    $param['field'] = 't.app_package';
                    $param['t.app_package'] = $param['pakName'];
                    $data = AppService::handleQueryTeacherApp($where,$param,$field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = AppService::handleQueryTeacherApp($where,$param,$field);
                    break;
            }
        }else{
            return ToolUtil::returnJson(400,'没有数据');
        }
        if(count($data['data'])){
            $result = self::checkDataAll($data);
            return ToolUtil::returnJson(200,'成功',$result['data'],$result['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编辑老师应用自启、联网、升级
     * @param $param
     * @return false|string
     */
    public static function editTeacherAppStart($param){
        $teacherApp = TeacherAppModel::queryFirst(['id'=>$param['id']],['bind_batch_id','app_package']);
        $batchArr = explode(',',$teacherApp['bind_batch_id']);
        $int = 0;
        $dataS = [];
        switch ($param['type']){
            case 1:
                if(isset($param['is_auto'])){
                    if($param['is_auto']){
                        $dataS['not'] = $teacherApp['app_package'];
                        $dataS['ban'] = '';
                    }else{
                        $dataS['not'] = '';
                        $dataS['ban'] = $teacherApp['app_package'];
                    }
                    $int = TeacherAppModel::edit(['id'=>$param['id']],['is_auto'=>$param['is_auto'],'update_time'=>time()]);
                    if($int){
                        $param['contentType'] = 19;
                        $param['remark'] = '应用自启';
                        $param['details'] = ToolUtil::handleSuccessOrError(['is_auto'=>$param['is_auto']?'启用自启':'禁用自启']);
                        LogUtil::addAdminLog($param);
                        $param['statusType'] = '应用自启';
                    }else{
                        $int = 0;
                    }
                }
                break;
            case 2:
                if(isset($param['is_network'])){
                    if($param['is_network']){
                        $dataS['not'] = $teacherApp['app_package'];
                        $dataS['ban'] = '';
                    }else{
                        $dataS['not'] = '';
                        $dataS['ban'] = $teacherApp['app_package'];
                    }
                    $int = TeacherAppModel::edit(['id'=>$param['id']],['is_network'=>$param['is_network'],'update_time'=>time()]);
                    if($int){
                        $param['contentType'] = 19;
                        $param['remark'] = '应用联网';
                        $param['details'] = ToolUtil::handleSuccessOrError(['is_network'=>$param['is_network']?'启用联网':'禁用联网']);
                        LogUtil::addAdminLog($param);
                        $param['statusType'] = '应用联网';
                    }else{
                        $int = 0;
                    }
                }
                break;
            case 3:
                if(isset($param['is_upgrade'])){
                    if($param['is_upgrade']){
                        $dataS['not'] = $teacherApp['app_package'];
                        $dataS['ban'] = '';
                    }else{
                        $dataS['not'] = '';
                        $dataS['ban'] = $teacherApp['app_package'];
                    }
                    $int = TeacherAppModel::edit(['id'=>$param['id']],['is_upgrade'=>$param['is_upgrade'],'update_time'=>time()]);
                    if($int){
                        $param['contentType'] = 19;
                        $param['remark'] = '应用升级';
                        $param['details'] = ToolUtil::handleSuccessOrError(['is_upgrade'=>$param['is_upgrade']?'启用升级':'禁用升级']);
                        LogUtil::addAdminLog($param);
                        $param['statusType'] = '应用升级';
                    }else{
                        $int = 0;
                    }
                }
                break;
            case 4:
                if(isset($param['is_slide'])){
                    if($param['is_slide']){
                        $dataS['not'] = $teacherApp['app_package'];
                        $dataS['ban'] = '';
                    }else{
                        $dataS['not'] = '';
                        $dataS['ban'] = $teacherApp['app_package'];
                    }
                    $int = TeacherAppModel::edit(['id'=>$param['id']],['is_slide'=>$param['is_slide'],'update_time'=>time()]);
                    if($int){
                        $param['contentType'] = 19;
                        $param['remark'] = '应用滑动';
                        $param['details'] = ToolUtil::handleSuccessOrError(['is_slide'=>$param['is_slide']?'启用滑动':'禁用滑动']);
                        LogUtil::addAdminLog($param);
                        $param['statusType'] = '应用滑动';
                    }else{
                        $int = 0;
                    }
                }
                break;
            default:
                return ToolUtil::returnJson(300,'失败');
        }
        if($int){
            $param['type'] = 1;
            $param['status'] = 1;
            $param['title'] = 'OK';
            $param['batch'] = $batchArr;
            $jPush = LogUtil::addPushLog($param,$dataS);
            if(in_array(200,$jPush)){
                return ToolUtil::returnJson(200,'成功');
            }
            return ToolUtil::returnJson(200,'成功,推送失败');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 获取家长应用自启、升级、联网
     * @param $param
     * @return false|string
     */
    public static function queryParentAppStart($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        $field = ['t.id','t.app_name','t.app_package','a.username','t.sn_value','t.app_img','t.is_auto',
            't.is_network','t.is_upgrade','t.is_slide'];
        if($admin['group_id']==1 || $admin['group_id']==2){
            switch ($param['type']){
                case 1:
                    $data = AppService::handleQueryParentApp([],$param,$field);
                    break;
                case 2:
                    $param['field'] = 't.app_name';
                    $param['t.app_name'] = $param['appName'];
                    $data = AppService::handleQueryParentApp([],$param,$field);
                    break;
                case 3:
                    $param['field'] = 't.app_package';
                    $param['t.app_package'] = $param['pakName'];
                    $data = AppService::handleQueryParentApp([],$param,$field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = AppService::handleQueryParentApp([],$param,$field);
                    break;
            }
        }elseif($admin['group_id']==4){
            $where['t.admin_id'] = $param['userId'];
            switch ($param['type']){
                case 1:
                    $data = AppService::handleQueryParentApp($where,$param,$field);
                    break;
                case 2:
                    $param['field'] = 't.app_name';
                    $param['t.app_name'] = $param['appName'];
                    $data = AppService::handleQueryParentApp($where,$param,$field);
                    break;
                case 3:
                    $param['field'] = 't.app_package';
                    $param['t.app_package'] = $param['pakName'];
                    $data = AppService::handleQueryParentApp($where,$param,$field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = AppService::handleQueryParentApp($where,$param,$field);
                    break;
            }
        }else{
            return ToolUtil::returnJson(400,'没有数据');
        }
        if(count($data['data'])){
            $result = self::handleSnName($data['data']);
            return ToolUtil::returnJson(200,'成功',$result,$data['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编辑家长应用自启、联网、升级
     * @param $param
     * @return false|string
     */
    public static function editParentAppStart($param){
        $teacherApp = ParentAppModel::queryFirst(['id'=>$param['id']],['sn_value','app_package']);
        $int = 0;
        $dataS = [];
        switch ($param['type']){
            case 1:
                if(isset($param['is_auto'])){
                    if($param['is_auto']){
                        $dataS['not'] = $teacherApp['app_package'];
                        $dataS['ban'] = '';
                    }else{
                        $dataS['not'] = '';
                        $dataS['ban'] = $teacherApp['app_package'];
                    }
                    $int = ParentAppModel::edit(['id'=>$param['id']],['is_auto'=>$param['is_auto'],'update_time'=>time()]);
                    if($int){
                        $param['contentType'] = 19;
                        $param['remark'] = '应用自启';
                        $param['details'] = ToolUtil::handleSuccessOrError(['is_auto'=>$param['is_auto']?'启用自启':'禁用自启']);
                        LogUtil::addAdminLog($param);
                        $param['statusType'] = '应用自启';
                    }else{
                        $int = 0;
                    }
                }
                break;
            case 2:
                if(isset($param['is_network'])){
                    if($param['is_network']){
                        $dataS['not'] = $teacherApp['app_package'];
                        $dataS['ban'] = '';
                    }else{
                        $dataS['not'] = '';
                        $dataS['ban'] = $teacherApp['app_package'];
                    }
                    $int = ParentAppModel::edit(['id'=>$param['id']],['is_network'=>$param['is_network'],'update_time'=>time()]);
                    if($int){
                        $param['contentType'] = 19;
                        $param['remark'] = '应用联网';
                        $param['details'] = ToolUtil::handleSuccessOrError(['is_network'=>$param['is_network']?'启用联网':'禁用联网']);
                        LogUtil::addAdminLog($param);
                        $param['statusType'] = '应用联网';
                    }else{
                        $int = 0;
                    }
                }
                break;
            case 3:
                if(isset($param['is_upgrade'])){
                    if($param['is_upgrade']){
                        $dataS['not'] = $teacherApp['app_package'];
                        $dataS['ban'] = '';
                    }else{
                        $dataS['not'] = '';
                        $dataS['ban'] = $teacherApp['app_package'];
                    }
                    $int = ParentAppModel::edit(['id'=>$param['id']],['is_upgrade'=>$param['is_upgrade'],'update_time'=>time()]);
                    if($int){
                        $param['contentType'] = 19;
                        $param['remark'] = '应用升级';
                        $param['details'] = ToolUtil::handleSuccessOrError(['is_upgrade'=>$param['is_upgrade']?'启用升级':'禁用升级']);
                        LogUtil::addAdminLog($param);
                        $param['statusType'] = '应用升级';
                    }else{
                        $int = 0;
                    }
                }
                break;
            case 4:
                if(isset($param['is_slide'])){
                    if($param['is_slide']){
                        $dataS['not'] = $teacherApp['app_package'];
                        $dataS['ban'] = '';
                    }else{
                        $dataS['not'] = '';
                        $dataS['ban'] = $teacherApp['app_package'];
                    }
                    $int = ParentAppModel::edit(['id'=>$param['id']],['is_slide'=>$param['is_slide'],'update_time'=>time()]);
                    if($int){
                        $param['contentType'] = 19;
                        $param['remark'] = '应用滑动';
                        $param['details'] = ToolUtil::handleSuccessOrError(['is_slide'=>$param['is_slide']?'启用滑动':'禁用滑动']);
                        LogUtil::addAdminLog($param);
                        $param['statusType'] = '应用滑动';
                    }else{
                        $int = 0;
                    }
                }
                break;
            default:
                return ToolUtil::returnJson(300,'失败');
        }
        if($int){
            $param['type'] = 2;
            $param['status'] = 1;
            $param['title'] = 'OK';
            $param['batch'] = array($teacherApp['sn_value']);
            $jPush = LogUtil::addPushLog($param);
            if(in_array(200,$jPush)){
                return ToolUtil::returnJson(200,'成功');
            }
            return ToolUtil::returnJson(200,'成功,推送失败');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 获取老师应用内部网址
     * @param $param
     * @return false|string
     */
    public static function queryTeacherAppInside($param)
    {
        $admin = AdminModel::queryFirst(['id' => $param['userId']], ['group_id']);
        $field = ['t.id', 't.app_name', 't.app_package', 'a.username', 't.bind_batch_id as batch_id', 't.address', 't.create_time'];
        if ($admin['group_id'] == 1 || $admin['group_id'] == 2) {
            switch ($param['type']) {
                case 1:
                    $data = AppService::handleQueryTeacherApp([], $param, $field);
                    break;
                case 2:
                    $param['field'] = 't.app_name';
                    $param['t.app_name'] = $param['appName'];
                    $data = AppService::handleQueryTeacherApp([], $param, $field);
                    break;
                case 3:
                    $param['field'] = 't.app_package';
                    $param['t.app_package'] = $param['pakName'];
                    $data = AppService::handleQueryTeacherApp([], $param, $field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = AppService::handleQueryTeacherApp([], $param, $field);
                    break;
            }
        }elseif ($admin['group_id'] == 3) {
            $where['t.admin_id'] = $param['userId'];
            switch ($param['type']) {
                case 1:
                    $data = AppService::handleQueryTeacherApp($where, $param, $field);
                    break;
                case 2:
                    $param['field'] = 't.app_name';
                    $param['t.app_name'] = $param['appName'];
                    $data = AppService::handleQueryTeacherApp($where, $param, $field);
                    break;
                case 3:
                    $param['field'] = 't.app_package';
                    $param['t.app_package'] = $param['pakName'];
                    $data = AppService::handleQueryTeacherApp($where, $param, $field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = AppService::handleQueryTeacherApp($where, $param, $field);
                    break;
            }
        }else{
            return ToolUtil::returnJson(400, '没有数据');
        }
        if(count($data['data'])){
            $result = self::checkDataAll($data);
            return ToolUtil::returnJson(200,'成功',$result['data'],$result['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编号获取老师应用内部网址
     * @param $param
     * @return false|string
     */
    public static function queryIdTeacherAppInside($param){
        $field = ['t.id', 't.app_name', 't.app_package', 'a.username', 't.bind_batch_id as batch_id', 't.address'];
        $data = TeacherAppModel::queryLeftFirst(['t.id'=>$param['id']],$field);
        if($data){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编辑老师应用内部网址
     * @param $param
     * @return false|string
     */
    public static function editTeacherAppInside($param){
        $teacherApp = TeacherAppModel::queryFirst(['id'=>$param['id']],['bind_batch_id','app_package']);
        if($teacherApp){
            $int = TeacherAppModel::edit(['id'=>$param['id']],['address'=>$param['address'],'update_time'=>time()]);
            if($int){
                $dataS['app_package'] = $teacherApp['app_package'];
                $param['remark'] = '编辑应用内部网址';
                $param['type'] = 1;
                $param['status'] = 1;
                $param['contentType'] = 16;
                $param['title'] = 'OK';
                $param['statusType'] = '编辑应用内部网址';
                $param['batch'] = explode(',',$teacherApp['bind_batch_id']);
                $param['details'] = ToolUtil::handleSuccessOrError(['address'=>$param['address']]);
                LogUtil::addAdminLog($param);
                $jPush = LogUtil::addPushLog($param,$dataS);
                if(in_array(200,$jPush)){
                    return ToolUtil::returnJson(200,'成功');
                }
                return ToolUtil::returnJson(200,'成功,推送失败');
            }
            return ToolUtil::returnJson(300,'失败');
        }
        return ToolUtil::returnJson(400,'没有该信息');
    }

    /**
     * 获取家长应用内部网址
     * @param $param
     * @return false|string
     */
    public static function queryParentAppInside($param)
    {
        $admin = AdminModel::queryFirst(['id' => $param['userId']], ['group_id']);
        $field = ['t.id', 't.app_name', 't.app_package', 'a.username', 't.sn_value', 't.address', 't.create_time'];
        if ($admin['group_id'] == 1 || $admin['group_id'] == 2) {
            switch ($param['type']) {
                case 1:
                    $data = AppService::handleQueryParentApp([], $param, $field);
                    break;
                case 2:
                    $param['field'] = 't.app_name';
                    $param['t.app_name'] = $param['appName'];
                    $data = AppService::handleQueryParentApp([], $param, $field);
                    break;
                case 3:
                    $param['field'] = 't.app_package';
                    $param['t.app_package'] = $param['pakName'];
                    $data = AppService::handleQueryParentApp([], $param, $field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = AppService::handleQueryParentApp([], $param, $field);
                    break;
            }
        }elseif ($admin['group_id'] == 4) {
            $where['t.admin_id'] = $param['userId'];
            switch ($param['type']) {
                case 1:
                    $data = AppService::handleQueryParentApp($where, $param, $field);
                    break;
                case 2:
                    $param['field'] = 't.app_name';
                    $param['t.app_name'] = $param['appName'];
                    $data = AppService::handleQueryParentApp($where, $param, $field);
                    break;
                case 3:
                    $param['field'] = 't.app_package';
                    $param['t.app_package'] = $param['pakName'];
                    $data = AppService::handleQueryParentApp($where, $param, $field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = AppService::handleQueryParentApp($where, $param, $field);
                    break;
            }
        }else{
            return ToolUtil::returnJson(400, '没有数据');
        }
        if(count($data['data'])){
            $result = self::handleSnName($data['data']);
            return ToolUtil::returnJson(200,'成功',$result,$data['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编号获取家长应用内部网址
     * @param $param
     * @return false|string
     */
    public static function queryIdParentAppInside($param){
        $field = ['t.id', 't.app_name', 't.app_package', 'a.username', 't.sn_value', 't.address'];
        $data = ParentAppModel::queryLeftFirst(['t.id'=>$param['id']],$field);
        if($data){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编辑家长应用内部网址
     * @param $param
     * @return false|string
     */
    public static function editParentAppInside($param){
        $parentApp = ParentAppModel::queryFirst(['id'=>$param['id']],['sn_value','app_package']);
        if($parentApp){
            $int = ParentAppModel::edit(['id'=>$param['id']],['address'=>$param['address'],'update_time'=>time()]);
            if($int){
                $dataS['app_package'] = $parentApp['app_package'];
                $param['remark'] = '编辑应用内部网址';
                $param['type'] = 2;
                $param['status'] = 1;
                $param['contentType'] = 16;
                $param['title'] = 'OK';
                $param['statusType'] = '编辑应用内部网址';
                $param['batch'] = array($parentApp['sn_value']);
                $param['details'] = ToolUtil::handleSuccessOrError(['address'=>$param['address']]);
                LogUtil::addAdminLog($param);
                $jPush = LogUtil::addPushLog($param,$dataS);
                if(in_array(200,$jPush)){
                    return ToolUtil::returnJson(200,'成功');
                }
                return ToolUtil::returnJson(200,'成功,推送失败');
            }
            return ToolUtil::returnJson(300,'失败');
        }
        return ToolUtil::returnJson(400,'没有该信息');
    }

    /**
     * 导入Tf卡信息
     * @param $param
     * @return false|string
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     */
    public static function importTfMedia($param){
        if(!ToolUtil::checkExcelSuffix($param)){
            return ToolUtil::returnJson(302,'文件类型必须为xlsx或xls');
        }
        $phpReader = new \PHPExcel_Reader_Excel2007();
        $phpExcel = $phpReader->load($param['file']);
        $currentSheet = $phpExcel->getSheet(0);//获取第一个表单
        $allRow = $currentSheet->getHighestRow();//获取总行数
        $maxColumn = $currentSheet->getHighestColumn();//获取最大列号
        $allColumn=\PHPExcel_Cell::columnIndexFromString($maxColumn);//获取总列数
        $formatArr = [];
        $typeArr = [];
        DB::beginTransaction();
        for ($currentRow=2;$currentRow<=$allRow;$currentRow++){
            for ($currentColumn=0;$currentColumn<$allColumn;$currentColumn++){
                $val = trim($currentSheet->getCellByColumnAndRow($currentColumn,$currentRow)->getValue());
                if($currentColumn==0){
                    $formatArr[$currentRow-2] = $val;
                }else{
                    $typeArr[$currentRow-2] = $val;
                }
            }
        }
        $errorArr = [];
        $success = 0;
        if(count($formatArr)!=0 && count($typeArr)!=0){
            foreach ($formatArr as $k=>$v){
                $tfId = TfMediaModel::queryValue(['format'=>$v,'tfmedia_id'=>$typeArr[$k]],'id');
                if($tfId){
                    $errorArr[$k+2] = $k+2;
                }else{
                    if(TfMediaModel::add(['format'=>$v,'tfmedia_id'=>$typeArr[$k]])){
                        $param['remark'] = '导入TF卡信息';
                        $param['details'] = ToolUtil::handleSuccessOrError(['format'=>$v]);
                        LogUtil::addAdminLog($param);
                        $success++;
                    }
                }
            }
        }else{
            DB::rollBack();
            return ToolUtil::returnJson(301,'格式不对');
        }
        DB::commit();
        $data['success'] = $success;
        $data['error'] = array_values($errorArr);
        $data['total'] = $allRow-1;
        return ToolUtil::returnJson(200,'成功',$data);
    }

    /**
     * 增加TF卡信息
     * @param $param
     * @return false|string
     */
    public static function addTfMedia($param){
        $tfId = TfMediaModel::queryValue(['format'=>$param['format'],'tfmedia_id'=>$param['typeId']],'id');
        if($tfId){
            return ToolUtil::returnJson(301,'存在相同类型');
        }
        $typeArr = [1,2,3,4];
        if(in_array($param['typeId'],$typeArr)){
            $int = TfMediaModel::add(['format'=>$param['format'],'tfmedia_id'=>$param['typeId']]);
            if($int){
                $param['remark'] = '增加TF卡';
                $param['details'] = ToolUtil::handleSuccessOrError(['format'=>$param['format']]);
                LogUtil::addAdminLog($param);
                return ToolUtil::returnJson(200,'成功');
            }
            return ToolUtil::returnJson(300,'失败');
        }
        return ToolUtil::returnJson(302,'分类有误');
    }

    /**
     * 获取TF卡信息
     * @param $param
     * @return false|string
     */
    public static function queryTfMedia($param){
        switch ($param['type']){
            case 1:
                $data = self::handleQueryTfMedia([],$param);
                break;
            case 2:
                $param['field'] = 't.format';
                $param['t.format'] = $param['format'];
                $data = self::handleQueryTfMedia([],$param);
                break;
            case 3:
                $param['field'] = 't.tfmedia_id';
                $param['t.tfmedia_id'] = $param['typeId'];
                $data = self::handleQueryTfMedia([],$param);
                break;
            default:
                $data['data'] = null;
        }
        if(count($data['data'])){
            return ToolUtil::returnJson(200,'成功',$data['data'],$data['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理获取TF卡信息
     * @param array $where
     * @param $param
     * @return mixed
     */
    public static function handleQueryTfMedia($where=[],$param){
        if($where){
            $data['count'] = TfMediaModel::queryCount($where,$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = TfMediaModel::queryAll($where,$param,$page);
        }else{
            $data['count'] = TfMediaModel::queryCount([],$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = TfMediaModel::queryAll([],$param,$page);
        }
        return $data;
    }

    /**
     * 编号获取TF卡信息
     * @param $param
     * @return false|string
     */
    public static function queryIdTfMedia($param){
        $data = TfMediaModel::queryFirst(['id'=>$param['id']],['id','format','tfmedia_id']);
        if($data){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编辑TF卡信息
     * @param $param
     * @return false|string
     */
    public static function editTfMedia($param){
        $id = TfMediaModel::queryValue(['format'=>$param['format'],'tfmedia_id'=>$param['typeId']],'id');
        if($id!=$param['id']){
            return ToolUtil::returnJson(301,'存在相同类型');
        }
        $int = TfMediaModel::edit(['id'=>$param['id']],['format'=>$param['format'],'tfmedia_id'=>$param['typeId']]);
        if($int){
            $param['remark'] = '编辑TF卡信息';
            $param['details'] = ToolUtil::handleSuccessOrError(['format'=>$param['format']]);
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 删除TF卡信息
     * @param $param
     * @return false|string
     */
    public static function deleteTfMedia($param){
        $tfMedia = TfMediaModel::queryFirst(['id'=>$param['id']],['format']);
        if($tfMedia){
            $int = TfMediaModel::delete(['id'=>$param['id']]);
            if($int){
                $param['remark'] = '删除TF卡信息';
                $param['details'] = ToolUtil::handleSuccessOrError(['format'=>$tfMedia['format']]);
                LogUtil::addAdminLog($param);
                return ToolUtil::returnJson(200,'成功');
            }
            return ToolUtil::returnJson(300,'失败');
        }
        return ToolUtil::returnJson(301,'没有该信息');
    }

    /**
     * 获取包名
     * @param $param
     * @return false|string
     */
    public static function queryPakName($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        if($admin['group_id']==3){
            $data = TeacherAppModel::queryGet(['admin_id'=>$param['userId']],['app_package','app_name']);
        }elseif ($admin['group_id']==4){
            $data = ParentAppModel::queryGet(['admin_id'=>$param['userId']],['app_package','app_name']);
        }else{
            return ToolUtil::returnJson(400,'没有数据');
        }
        if(count($data)){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 类型获取TF卡信息
     * @param $param
     * @return false|string
     */
    public static function queryTypeTfMedia($param){
        $data = TfMediaModel::queryGet(['tfmedia_id'=>$param['type']],['format']);
        if(count($data)){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 增加老师系统其它管控
     * @param $param
     * @return false|string
     */
    public static function addTeacherSetting($param){

        $param['bId'] = ToolUtil::filterStr($param['bId']);
        $batchArr = explode(',',$param['bId']);
        foreach ($batchArr as $key => $value) {
            $t_setting = DB::table('t_setting')
                ->whereRaw(" find_in_set({$value},batch_ids) ")
                ->first(['id']);

            if($t_setting){
                return ToolUtil::returnJson(301,'存在相同班级');
            }
        }

        $formatArr = explode(',',$param['setting_tfmedia_format']);
        $formatIdArr = [];
        foreach ($formatArr as $k=>$v){
            $formatIdArr[$k] = TfMediaModel::queryValue(['format'=>$v],'id');
        }
        $formatStr = implode(',',$formatIdArr);
        $data = [
            'batch_ids'=>$param['bId'],
            'admin_id'=>$param['userId'],
            'setting_tfmedia_format'=>$formatStr,
            'create_time'=>time(),
            'setting_call'=>$param['setting_call'],
            'setting_memory'=>$param['setting_memory'],
            'setting_usb'=>$param['setting_usb'],
            //'setting_bluetooth'=>$param['setting_bluetooth'],
            'setting_navigation'=>$param['setting_navigation'],
            'setting_statusbar'=>$param['setting_statusbar'],
            'setting_tfmedia'=>$param['setting_tfmedia'],
            'setting_phones'=>$param['setting_phones'],
            'setting_phone'=>$param['setting_phone'],
            'setting_camera'=>$param['setting_camera'],
            'setting_clock'=>$param['setting_clock'],
            'setting_recording'=>$param['setting_recording'],
            'setting_music'=>$param['setting_music'],
            'setting_picture'=>$param['setting_picture'],
            'setting_wallpaper'=>$param['setting_wallpaper'],
            'setting_file'=>$param['setting_file'],
            //'setting_bhtvideo'=>$param['setting_bhtvideo'],
            //'setting_context'=>$param['setting_context'],
            'setting_bht'=>$param['setting_bht'],
            'setting_hotspot'=>$param['setting_hotspot'],
            'qch_restore'=>$param['qch_restore'],
            'setting_browserInput'=>$param['setting_browserInput'],
            'setting_browser'=>$param['setting_browser'],
            'dev_mode'=>$param['dev_mode']
        ];
        if(SettingModel::add($data)){
            $param['type'] = 1;
            $param['status'] = 1;
            $param['title'] = 'OK';
            $param['remark'] = '增加系统设置';
            $param['contentType'] = 21;
            $param['statusType'] = '增加系统设置';
            $param['batch'] = $batchArr;
            $param['details'] = ToolUtil::handleSuccessOrError(['batch'=>$param['bId']]);
            LogUtil::addAdminLog($param);
            $data['setting_tfmedia_format'] = $param['setting_tfmedia_format'];
            $jPush = LogUtil::addPushLog($param,$data);
            if(in_array(200,$jPush)){
                return ToolUtil::returnJson(200,'成功');
            }
            return ToolUtil::returnJson(200,'成功,推送失败');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 获取老师系统其它管控
     * @param $param
     * @return false|string
     */
    public static function queryTeacherSetting($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        $field = ['s.id','s.batch_ids as batch_id','a.username','s.setting_tfmedia_format','s.create_time','s.setting_call',
            's.setting_memory','s.setting_usb','s.setting_navigation','s.setting_statusbar',
            's.setting_tfmedia','s.setting_phones','s.setting_phone','s.setting_camera','s.setting_clock',
            's.setting_recording','s.setting_music','s.setting_picture','s.setting_wallpaper',
            's.setting_file', 's.setting_bht','s.setting_hotspot','s.qch_restore','s.setting_browserInput','s.setting_browser','s.dev_mode'];
        if($admin['group_id']==1 || $admin['group_id']==2){
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryTeacherSetting([],$param,$field);
                    break;
                default:
                    $data['data'] = null;
            }
        }elseif($admin['group_id']==3){
            $where['s.admin_id'] = $param['userId'];
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryTeacherSetting($where,$param,$field);
                    break;
                default:
                    $data['data'] = null;
            }
        }else{
            return ToolUtil::returnJson(400,'没有数据');
        }
        if(count($data['data'])){
            $result = self::checkDataAll($data);
            return ToolUtil::returnJson(200,'成功',$result['data'],$result['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理获取老师系统其它管控
     * @param $where
     * @param $param
     * @param $field
     * @return mixed
     */
    public static function handleQueryTeacherSetting($where,$param,$field){
        if($where){
            $data['count'] = SettingModel::queryCount($where,$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = SettingModel::queryAll($where,$param,$page,$field);
        }else{
            $data['count'] = SettingModel::queryCount([],$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = SettingModel::queryAll([],$param,$page,$field);
        }
        return $data;
    }

    /**
     * 编号获取老师系统其它管控
     * @param $param
     * @return false|string
     */
    public static function queryIdTeacherSetting($param){
        $field = ['s.id','s.batch_ids as batch_id','a.username','s.setting_tfmedia_format','s.create_time','s.setting_call',
            's.setting_memory','s.setting_usb','s.setting_navigation','s.setting_statusbar',
            's.setting_tfmedia','s.setting_phones','s.setting_phone','s.setting_camera','s.setting_clock',
            's.setting_recording','s.setting_music','s.setting_picture','s.setting_wallpaper',
            's.setting_file','s.setting_bht','s.setting_hotspot','s.qch_restore','s.setting_browserInput','s.setting_browser','s.dev_mode'];
        $data = SettingModel::queryLeftFirst(['s.id'=>$param['id']],$field);
        if ($data){
            $result = self::handleFormat($data);
            return ToolUtil::returnJson(200,'成功',$result);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编辑老师系统其它管控
     * @param $param
     * @return false|string
     */
    public static function editTeacherSetting($param){

        $param['id'] = (int)$param['id'];
        $settingBatch = SettingModel::queryFirst(['id'=>$param['id']],['batch_ids']);
        if(empty($settingBatch)){
            return ToolUtil::returnJson(301,'没有记录');
        }
        $param['bId'] = ToolUtil::filterStr($param['bId']);
        $batchArr = explode(',',$param['bId']);
        foreach ($batchArr as $key => $value) {
            $t_setting = DB::table('t_setting')
                ->whereRaw(" find_in_set({$value},batch_ids) ")
                ->first(['id']);
            if($t_setting['id'] != $param['id']){
                return ToolUtil::returnJson(301,'存在相同班级');
            }
        }

        $formatArr = explode(',',$param['setting_tfmedia_format']);
        $formatIdArr = [];
        foreach ($formatArr as $k=>$v){
            $formatIdArr[$k] = TfMediaModel::queryValue(['format'=>$v],'id');
        }
        $formatStr = implode(',',$formatIdArr);
        $data = [
            'batch_ids'=>$param['bId'],
            'setting_tfmedia_format'=>$formatStr,
            'update_time'=>time(),
            'setting_call'=>$param['setting_call'],
            'setting_memory'=>$param['setting_memory'],
            'setting_usb'=>$param['setting_usb'],
            //'setting_bluetooth'=>$param['setting_bluetooth'],
            'setting_navigation'=>$param['setting_navigation'],
            'setting_statusbar'=>$param['setting_statusbar'],
            'setting_tfmedia'=>$param['setting_tfmedia'],
            'setting_phones'=>$param['setting_phones'],
            'setting_phone'=>$param['setting_phone'],
            'setting_camera'=>$param['setting_camera'],
            'setting_clock'=>$param['setting_clock'],
            'setting_recording'=>$param['setting_recording'],
            'setting_music'=>$param['setting_music'],
            'setting_picture'=>$param['setting_picture'],
            'setting_wallpaper'=>$param['setting_wallpaper'],
            'setting_file'=>$param['setting_file'],
            //'setting_bhtvideo'=>$param['setting_bhtvideo'],
            //'setting_context'=>$param['setting_context'],
            'setting_bht'=>$param['setting_bht'],
            'setting_hotspot'=>$param['setting_hotspot'],
            'qch_restore'=>$param['qch_restore'],
            'setting_browserInput'=>$param['setting_browserInput'],
            'setting_browser'=>$param['setting_browser'],
            'dev_mode'=>$param['dev_mode']
        ];
        if(SettingModel::edit(['id'=>$param['id']],$data)){
            $param['type'] = 1;
            $param['status'] = 1;
            $param['title'] = 'OK';
            $param['remark'] = '编辑系统设置';
            $param['contentType'] = 21;
            $param['statusType'] = '编辑系统设置';
            $param['details'] = ToolUtil::handleSuccessOrError(['batch'=>$param['bId']]);
            LogUtil::addAdminLog($param);
            
            $batchIdArr2 = explode(',',$settingBatch['batch_ids']);
            $batchIdArr3 = array_diff($batchIdArr2,$batchArr);
            if($batchIdArr3){
                $data2=[
                    "setting_call"=> 1,
                    "setting_memory"=> 1,
                    "setting_usb"=> "usb_midi",
                    "setting_bluetooth"=> 1,
                    "setting_navigation"=> 1,
                    "setting_statusbar"=> 1,
                    "setting_tfmedia"=> 1,
                    "setting_phone"=> 1,
                    "setting_camera"=> 1,
                    "setting_clock"=> 1,
                    "setting_recording"=> 1,
                    "setting_music"=> 1,
                    "setting_picture"=> 1,
                    "setting_wallpaper"=> 1,
                    "setting_file"=> 1,
                    "setting_bhtvideo"=>1,
                    "setting_bht"=>1,
                    "setting_hotspot"=>1,
                    "qch_restore"=>1,
                    "setting_browserInput"=>1,
                    "setting_browser"=>1,
                    "dev_mode"=>1
                ];
                $param['batch'] = $batchIdArr3;
                LogUtil::addPushLog($param,$data2);
            }
            
            $param['batch'] = $batchArr;
            $data['setting_tfmedia_format'] = $param['setting_tfmedia_format'];
            $jPush = LogUtil::addPushLog($param,$data);
            if(in_array(200,$jPush)){
                return ToolUtil::returnJson(200,'成功');
            }
            return ToolUtil::returnJson(200,'成功,推送失败');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 删除老师系统其它管控
     * @param $param
     * @return false|string
     */
    public static function deleteTeacherSetting($param){

        try{
            $param['id'] = (int)$param['id'];
            $setting = SettingModel::queryFirst(['id'=>$param['id']],['batch_ids']);
            if(empty($setting)){
                return ToolUtil::returnJson(301,'不存在该信息');
            }

            $batchArr = explode(',',$setting['batch_ids']);
            
            if(SettingModel::delete(['id'=>$param['id']])){
                $param['type'] = 1;
                $param['status'] = 1;
                $param['title'] = 'OK';
                $param['remark'] = '删除系统设置';
                $param['contentType'] = 21;
                $param['statusType'] = '删除系统设置';
                $param['batch'] = $batchArr;
                $param['details'] = ToolUtil::handleSuccessOrError($batchArr);
                $data = [
                    "setting_call"=> 1,
                    "setting_memory"=> 1,
                    "setting_usb"=> "usb_charge",
                    "setting_bluetooth"=> 1,
                    "setting_navigation"=> 1,
                    "setting_statusbar"=> 1,
                    "setting_tfmedia"=> 1,
                    "setting_phone"=> 1,
                    "setting_camera"=> 1,
                    "setting_clock"=> 1,
                    "setting_recording"=> 1,
                    "setting_music"=> 1,
                    "setting_picture"=> 1,
                    "setting_wallpaper"=> 1,
                    "setting_file"=> 1,
                    "setting_bhtvideo"=>1,
                    "setting_bht"=>1,
                    "setting_hotspot"=>1,
                    "qch_restore"=>1,
                    "setting_browserInput"=>1,
                    "setting_browser"=>1,
                    "dev_mode"=>1
                ];
                LogUtil::addAdminLog($param);

                if(!empty($batchArr)){
                    $jPush = LogUtil::addPushLog($param,$data);
                    if(in_array(200,$jPush)){
                        return ToolUtil::returnJson(200,'成功');
                    }
                    return ToolUtil::returnJson(200,'成功,推送失败');
                }else{
                    return ToolUtil::returnJson(200,'成功');
                }

            }
            return ToolUtil::returnJson(300,'失败');
            
        }catch (\Exception $exception){
            DB::rollBack();
            return ToolUtil::returnJson(500,$exception->getMessage());
        }

    }

    /**
     * 增加家长系统其它管控
     * @param $param
     * @return false|string
     */
    public static function addParentSetting($param){

        $setting = SettingModel::queryFirst(['sn_value'=>$param['snName']],['id']);
        if($setting){
            return ToolUtil::returnJson(301,'存在该设备信息');
        }
        $formatArr = explode(',',$param['setting_tfmedia_format']);
        $formatIdArr = [];
        foreach ($formatArr as $k=>$v){
            $formatIdArr[$k] = TfMediaModel::queryValue(['format'=>$v],'id');
        }
        $formatStr = implode(',',$formatIdArr);
        $data = [
            'sn_value'=>$param['snName'],
            'admin_id'=>$param['userId'],
            'setting_tfmedia_format'=>$formatStr,
            'create_time'=>time(),
            'setting_call'=>$param['setting_call'],
            'setting_memory'=>$param['setting_memory'],
            'setting_usb'=>$param['setting_usb'],
            //'setting_bluetooth'=>$param['setting_bluetooth'],
            'setting_navigation'=>$param['setting_navigation'],
            'setting_statusbar'=>$param['setting_statusbar'],
            'setting_tfmedia'=>$param['setting_tfmedia'],
            'setting_phones'=>$param['setting_phones'],
            'setting_phone'=>$param['setting_phone'],
            'setting_camera'=>$param['setting_camera'],
            'setting_clock'=>$param['setting_clock'],
            'setting_recording'=>$param['setting_recording'],
            'setting_music'=>$param['setting_music'],
            'setting_picture'=>$param['setting_picture'],
            'setting_wallpaper'=>$param['setting_wallpaper'],
            'setting_file'=>$param['setting_file'],
            //'setting_bhtvideo'=>$param['setting_bhtvideo'],
            //'setting_context'=>$param['setting_context'],
            'setting_bht'=>$param['setting_bht'],
            'setting_hotspot'=>$param['setting_hotspot'],
            'qch_restore'=>$param['qch_restore'],
            'setting_browserInput'=>$param['setting_browserInput'],
            'setting_browser'=>$param['setting_browser'],
            'dev_mode'=>$param['dev_mode']
        ];
        if(SettingModel::add($data)){
            $param['type'] = 2;
            $param['status'] = 1;
            $param['title'] = 'OK';
            $param['remark'] = '增加系统设置';
            $param['contentType'] = 21;
            $param['statusType'] = '增加系统设置';
            $param['batch'] = array($param['snName']);
            $param['details'] = ToolUtil::handleSuccessOrError(['snValue'=>$param['snName']]);
            LogUtil::addAdminLog($param);
            $data['setting_tfmedia_format'] = $param['setting_tfmedia_format'];
            $jPush = LogUtil::addPushLog($param,$data);
            if(in_array(200,$jPush)){
                return ToolUtil::returnJson(200,'成功');
            }
            return ToolUtil::returnJson(200,'成功,推送失败');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 获取家长系统其它管控
     * @param $param
     * @return false|string
     */
    public static function queryParentSetting($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        $field = ['s.id','s.sn_value','a.username','s.setting_tfmedia_format','s.create_time','s.setting_call',
            's.setting_memory','s.setting_usb','s.setting_bluetooth','s.setting_navigation','s.setting_statusbar',
            's.setting_tfmedia','s.setting_phones','s.setting_phone','s.setting_camera','s.setting_clock',
            's.setting_recording','s.setting_music','s.setting_picture','s.setting_wallpaper',
            's.setting_file','s.setting_bhtvideo', 's.setting_context', 's.setting_bht','s.setting_hotspot','s.qch_restore','s.setting_browserInput','s.setting_browser','s.dev_mode'];
        if($admin['group_id']==1 || $admin['group_id']==2){
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryTeacherSetting([],$param,$field);
                    break;
                default:
                    $data['data'] = null;
            }
        }elseif($admin['group_id']==4){
            $where['s.admin_id'] = $param['userId'];
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryTeacherSetting($where,$param,$field);
                    break;
                default:
                    $data['data'] = null;
            }
        }else{
            return ToolUtil::returnJson(400,'没有数据');
        }
        if(count($data['data'])){
            $result = self::handleSnName($data['data']);
            return ToolUtil::returnJson(200,'成功',$result,$data['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编号获取家长系统其它管控
     * @param $param
     * @return false|string
     */
    public static function queryIdParentSetting($param){
        $field = ['s.id','s.sn_value','a.username','s.setting_tfmedia_format','s.create_time','s.setting_call',
            's.setting_memory','s.setting_usb','s.setting_bluetooth','s.setting_navigation','s.setting_statusbar',
            's.setting_tfmedia','s.setting_phones','s.setting_phone','s.setting_camera','s.setting_clock',
            's.setting_recording','s.setting_music','s.setting_picture','s.setting_wallpaper',
            's.setting_file','s.setting_bhtvideo', 's.setting_context', 's.setting_bht','s.setting_hotspot','s.qch_restore','s.setting_browserInput','s.setting_browser','s.dev_mode'];
        $data = SettingModel::queryLeftFirst(['s.id'=>$param['id']],$field);
        if ($data){
            $result = self::handleFormat($data);
            return ToolUtil::returnJson(200,'成功',$result);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理Tf卡信息
     * @param $data
     * @return mixed
     */
    public static function handleFormat($data){
        $fArr = explode(',',$data['setting_tfmedia_format']);
        $tArr = [];
        foreach ($fArr as $k=>$v){
            $tArr[$k] = TfMediaModel::queryFirst(['id'=>$v],['format','tfmedia_id']);
        }
        $data['setting_tfmedia_format'] = $tArr;
        return $data;
    }

    /**
     * 编辑家长系统其它管控
     * @param $param
     * @return false|string
     */
    public static function editParentSetting($param){

        $setting = SettingModel::queryFirst(['sn_value'=>$param['snName']],['id']);
        if($setting){
            if($setting['id']!=$param['id']){
                return ToolUtil::returnJson(301,'存在该设备信息');
            }
        }
        $formatArr = explode(',',$param['setting_tfmedia_format']);
        $formatIdArr = [];
        foreach ($formatArr as $k=>$v){
            $formatIdArr[$k] = TfMediaModel::queryValue(['format'=>$v],'id');
        }
        $formatStr = implode(',',$formatIdArr);
        $data = [
            'sn_value'=>$param['snName'],
            'setting_tfmedia_format'=>$formatStr,
            'update_time'=>time(),
            'setting_call'=>$param['setting_call'],
            'setting_memory'=>$param['setting_memory'],
            'setting_usb'=>$param['setting_usb'],
            //'setting_bluetooth'=>$param['setting_bluetooth'],
            'setting_navigation'=>$param['setting_navigation'],
            'setting_statusbar'=>$param['setting_statusbar'],
            'setting_tfmedia'=>$param['setting_tfmedia'],
            'setting_phones'=>$param['setting_phones'],
            'setting_phone'=>$param['setting_phone'],
            'setting_camera'=>$param['setting_camera'],
            'setting_clock'=>$param['setting_clock'],
            'setting_recording'=>$param['setting_recording'],
            'setting_music'=>$param['setting_music'],
            'setting_picture'=>$param['setting_picture'],
            'setting_wallpaper'=>$param['setting_wallpaper'],
            'setting_file'=>$param['setting_file'],
            //'setting_bhtvideo'=>$param['setting_bhtvideo'],
            //'setting_context'=>$param['setting_context'],
            'setting_bht'=>$param['setting_bht'],
            'setting_hotspot'=>$param['setting_hotspot'],
            'qch_restore'=>$param['qch_restore'],
            'setting_browserInput'=>$param['setting_browserInput'],
            'setting_browser'=>$param['setting_browser'],
            'dev_mode'=>$param['dev_mode']
        ];
        if(SettingModel::edit(['id'=>$param['id']],$data)){
            $param['type'] = 2;
            $param['status'] = 1;
            $param['title'] = 'OK';
            $param['remark'] = '编辑系统设置';
            $param['contentType'] = 21;
            $param['statusType'] = '编辑系统设置';
            $param['batch'] = array($param['snName']);
            $param['details'] = ToolUtil::handleSuccessOrError(['snValue'=>$param['snName']]);
            LogUtil::addAdminLog($param);
            $data['setting_tfmedia_format'] = $param['setting_tfmedia_format'];
            $jPush = LogUtil::addPushLog($param,$data);
            if(in_array(200,$jPush)){
                return ToolUtil::returnJson(200,'成功');
            }
            return ToolUtil::returnJson(200,'成功,推送失败');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 删除家长系统其它管控
     * @param $param
     * @return false|string
     */
    public static function deleteParentSetting($param){
        $setting = SettingModel::queryFirst(['id'=>$param['id']],['sn_value']);
        if($setting){
            if(SettingModel::delete(['id'=>$param['id']])){
                $param['type'] = 2;
                $param['status'] = 1;
                $param['title'] = 'OK';
                $param['remark'] = '删除系统设置';
                $param['contentType'] = 21;
                $param['statusType'] = '删除系统设置';
                $param['batch'] = array($setting['sn_value']);
                $param['details'] = ToolUtil::handleSuccessOrError(['snValue'=>$setting['sn_value']]);
                $data = [
                    "setting_call"=> 1,
                    "setting_memory"=> 1,
                    "setting_usb"=> "usb_charge",
                    "setting_bluetooth"=> 1,
                    "setting_navigation"=> 1,
                    "setting_statusbar"=> 1,
                    "setting_tfmedia"=> 1,
                    "setting_phone"=> 1,
                    "setting_camera"=> 1,
                    "setting_clock"=> 1,
                    "setting_recording"=> 1,
                    "setting_music"=> 1,
                    "setting_picture"=> 1,
                    "setting_wallpaper"=> 1,
                    "setting_file"=> 1,
                    "setting_bhtvideo"=>1,
                    "setting_bht"=>1,
                    "setting_hotspot"=>1,
                    "qch_restore"=>1,
                    "setting_browserInput"=>1,
                    "setting_browser"=>1,
                    "dev_mode"=>1
                ];
                LogUtil::addAdminLog($param);
                $jPush = LogUtil::addPushLog($param,$data);
                if(in_array(200,$jPush)){
                    return ToolUtil::returnJson(200,'成功');
                }
                return ToolUtil::returnJson(200,'成功,推送失败');
            }
            return ToolUtil::returnJson(300,'失败');
        }
        return ToolUtil::returnJson(301,'不存在该信息');
    }
}